//
//  ViewController.m
//  10-CustomCell-易车
//
//  Created by LL.F on 16/7/27.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "ViewController.h"
#import "CycleImages.h"
#import "SYView.h"
#import "ImageTableViewCell.h"
#import "NormolTableViewCell.h"
#import "CellModel.h"
#define kScreenbounds [UIScreen mainScreen].bounds
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight  [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *cycleArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.根据提供的Plist数据完成简易版易车界面（无图片显示)
    //注意: plist文件读取出来是个NSDictionary, 打印之后根据内容解析转换成相应的类
    //2.根据提供的Plist数据完成完整版易车界面（有图片显示并调研SDWebImage）
    
    // 初始化tableview
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerClass:[ImageTableViewCell class] forCellReuseIdentifier:@"imageCell"];
    [_tableView registerClass:[NormolTableViewCell class] forCellReuseIdentifier:@"normolCell"];
    
    
    self.dataSource = [NSMutableArray array];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YiChe" ofType:@"plist"];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    NSDictionary *dataDic = [dictionary objectForKey:@"data"];
    NSArray *listArray = [dataDic objectForKey:@"list"];
    for (NSDictionary *dic in listArray) {
        CellModel *model = [[CellModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataSource addObject:model];
    }
    //NSLog(@"%@", _dataSource);
    
        self.cycleArray = [NSMutableArray array];
    NSArray *cycleImageArray = [dataDic objectForKey:@"cycleImage"];
    for (NSDictionary *dic in cycleImageArray) {
        CellModel *model = [[CellModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        NSLog(@"%@", model.picCover);
        [self.cycleArray addObject:model.picCover];
    }
    [_cycleArray removeLastObject];
    NSLog(@"%@", _cycleArray);

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _dataSource.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell"];
        [cell.SYView1 setWithButtonImage:@"1" bottomLabelText:@"贷款买车"];
        [cell.SYView2 setWithButtonImage:@"2" bottomLabelText:@"直销车型"];
        [cell.SYView3 setWithButtonImage:@"3" bottomLabelText:@"低价买车"];
        [cell.SYView4 setWithButtonImage:@"4" bottomLabelText:@"二手车"];

    return cell;
    } else {
        NormolTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"normolCell"];
        CellModel *model = [self.dataSource objectAtIndex:indexPath.row];
        cell.model = model;
        
        return cell;
    }

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 0) {
        
        return  100;
    } else
        
        return 80;
        
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  
    return 1;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   
   
   CycleImages *cycleImage = [[CycleImages alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
    cycleImage.cycleScrollView.showsHorizontalScrollIndicator = YES;
    [self.view addSubview:cycleImage];
    NSArray *array = [NSArray arrayWithArray:_cycleArray];
    [cycleImage setImagesWithArray:array];
    return cycleImage;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 200;
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
